<?php
/**
 * Created by PhpStorm.
 * User: zhouxin
 * Date: 2018/10/5
 * Time: 10:27
 */

include "config.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST["user_id"]) && isset($_POST["pro_num"]) && isset($_POST["pro_price"]) && isset($_POST["pro_id"]))
    {
        $query = "SELECT*FROM `cart` WHERE user_id =? AND pro_id=?";
        $stmt = $conn->prepare($query);
        $stmt->bind_param("ii", $_POST["user_id"], $_POST["pro_id"]);
        $stmt->execute();
        $queryResult = $stmt->get_result();
        if ($queryResult->num_rows >= 1) {
            $update = "UPDATE cart SET pro_num=pro_num+? WHERE user_id=? AND pro_id=?;";
            $stmt = $conn->prepare($update);
            $stmt->bind_param("iii", $_POST["pro_num"],$_POST["user_id"], $_POST["pro_id"]);
            $stmt->execute();
            if ($stmt->affected_rows >= 1) {
                print_r(json_encode(array("status" => 2, "msg" => "加入成功u")));
            } else {
                print_r(json_encode(array("status" => -2, "msg" => "加入失败u")));
            }
        } else {
            $insert = "INSERT INTO cart (`user_id`,`pro_id`,`pro_num`,`pro_price`,`pro_name`,`pro_imgPath`)VALUE(?,?,?,?,?,?);";
            $stmt = $conn->prepare($insert);
            $stmt->bind_param("iiiiss", $_POST["user_id"], $_POST["pro_id"], $_POST["pro_num"], $_POST["pro_price"], $_POST["pro_name"], $_POST["pro_imgPath"]);
            $stmt->execute();
            if ($stmt->affected_rows >= 1) {
                print_r(json_encode(array("status" => 1, "msg" => "加入成功i")));
            } else {
                print_r(json_encode(array("status" => -1, "msg" => "加入失败")));
            }
        }
    }
else{
    print_r(json_encode(array("status" => 0, "msg" => "参数错误")));
    }


}